﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Http;
using System.IO;
using System.Text;
using Shop.Common;

namespace Shop
{
    public class Index
    {
        #region 第一次调用的主目录
        public static HttpResponseMessage mainswith()
        {
            try
            {
                string path = HttpContext.Current.Request.Url.AbsolutePath;
                string[] switchNames = path.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                //if (switchNames.Length <=1) {  return Common.HandleJson.ToJson(Common.ReturnValue.ErrAction()); }
                string mainname = switchNames[1];
                Dictionary<string, object> dic = new Dictionary<string, object>();
                switch (mainname)
                {
                    #region 客户档案处理
                    case "Customer":
                        dic = Interface.Customer.ICustomer.Switch(switchNames);
                        break;
                    case "CustomerConsignee":
                        dic = Interface.Customer.ICustomerConsignee.Switch(switchNames);
                        break;
                    case "CustomerFavor":
                        dic = Interface.Customer.ICustomerFavor.Switch(switchNames);
                        break;
                    case "Coupon":
                        dic = Interface.Customer.ICoupon.Switch(switchNames);
                        break;
                    #endregion
                    #region 存货档案处理
                    case "Inventory":
                        dic = Interface.Inventory.IInventory.Switch(switchNames);
                        break;
                    #endregion
                    #region 订单处理
                    case "SO":
                        dic = Interface.SO.ISO.Switch(switchNames);
                        break;
                    #endregion
                    default:
                        dic = Common.ReturnValue.ErrAction();
                        dic.Add("url", HttpContext.Current.Request.Url.AbsoluteUri);
                        dic.Add("urlold", HttpContext.Current.Request.Url.OriginalString);
                        break;
                }
                return Common.HandleJson.ToJson(dic);
            }
            catch (Exception ex)
            {
                System.Reflection.MethodBase.GetCurrentMethod().WriteErrLog(ex.Message + Environment.NewLine + ex.StackTrace);
                return Common.HandleJson.ToJson("出现错误，请与我公司4008237007联系处理，谢谢！", false);
            }
        }
        #endregion

        #region 接受微信的消息
        public static HttpResponseMessage getWeiXin()
        {
            MethodBaseExt.WriteErrLog(System.Reflection.MethodBase.GetCurrentMethod(),"接收微信服务器消息");
            //获取流
            Stream s = System.Web.HttpContext.Current.Request.InputStream;
            //转换成Byte数组
            byte[] b = new byte[s.Length];
            //读取流
            s.Read(b, 0, (int)s.Length);
            //转化成utf8编码
            //test--test123456           
            string postStr = Encoding.UTF8.GetString(b);
            System.Reflection.MethodBase.GetCurrentMethod().WriteErrLog("接受微信服务器消息成功：" + postStr + Environment.NewLine);
            return HandleJson.ToJson("接受微信服务器消息成功", true);
        }
        #endregion
    }
}